package com.weilive.pay.dao.mapper;

import java.util.Date;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.weilive.pay.dao.model.AddEMoney;
import com.weilive.pay.dao.model.CashLog;
import com.weilive.pay.dao.model.UserCash;
import com.weilive.pay.dao.model.UserInfo;

public interface UserCashMapper {
	int deleteByPrimaryKey(Long tradeId);

	int insert(UserCash record);

	int insertSelective(UserCash record);

	UserCash selectByPrimaryKey(Long tradeId);

	int updateByPrimaryKeySelective(UserCash record);

	int updateByPrimaryKey(UserCash record);

	@Select("select * from tx_user_cash where out_trade_no = #{outTradeNo}")
	@ResultMap("com.weilive.pay.dao.mapper.UserCashMapper.BaseResultMap")
	UserCash getUserCash(@Param("outTradeNo") String outTradeNo);

	@Select("call add_e_money_cash(#{userId}, #{money}, #{tradeId})")
	@Results(value = { @Result(property = "balance", column = "balance"), @Result(property = "err_code", column = "err_code"), @Result(property = "err_msg", column = "err_msg"), @Result(property = "is_success_flag", column = "is_success_flag") })
	AddEMoney addEMoney(@Param("userId") long userId, @Param("money") long money, @Param("tradeId") long tradeId);

//	@Select("select e_money from user_info where user_id = #{userId}")
//	int getEMoney(@Param("userId") long userId);
	
	
//	@Select("select vip_expiration from user_info where user_id = #{userId}")
//	Date getUserVipDate(@Param("userId") long userId);

	@Select("select id, user_id, status from l_cash_log where id = #{id}")
	@ResultMap("com.weilive.pay.dao.mapper.UserCashMapper.CashLogMap")
	CashLog getCashLog(@Param("id") String id);

	@Insert("insert into l_cash_log (id, user_id, recipe, status) values (#{log.id}, #{log.user_id}, #{log.recipe}, #{log.status})")
	int saveCashLog(@Param("log") CashLog log);

	@Update("update l_cash_log set status = 1 where id = #{id}")
	int useCashSuccess(@Param("id") String id);

	@Update("update user_info set vip_expiration = #{vipDate} where user_id = #{userId}")
	int setUserVipDate(@Param("userId") long userId, @Param("vipDate") Date vipDate);
	
	@Select("select e_money, vip_expiration from user_info where user_id = #{userId}")
	@Results(value = { @Result(property = "e_money", column = "e_money"), @Result(property = "vip_expiration", column = "vip_expiration")})
	UserInfo getUserInfo(@Param("userId") long userId);

}